Emit flat JSON log records without redundant text field#34
Merged
Conversation
Loguru's default serialize=True produces {"text": "...\n", "record":
{...}} where the text field is a pre-formatted duplicate of the
structured data with an embedded newline. Log aggregators then
surface literal \n escapes in the JSON, making entries harder to
read and redundant with per-line framing.
Replace the serialize=True sink with a custom JSON sink that emits a
flat record: time, level, message, module, function, line, name, and
the bind()-supplied extra as a nested dict. No text field, no
embedded newlines.
Update test_logging.py to exercise configure_logging(json_output=True)
directly and assert on the flat structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Loguru's default
serialize=Trueproduces{"text": "...\n", "record": {...}}where thetextfield is a pre-formatted duplicate of the structured data with an embedded newline. Log aggregators then surface literal\nescapes in the JSON body, making entries harder to read and redundant with per-line framing.Replace the
serialize=Truesink with a custom JSON sink that emits a flat record with just the useful fields:time,level,message,module,function,line,name, plusextra(fromlogger.bind()). Notextfield, no embedded newlines.Before
{"text": "2026-04-17 05:24:29.387 | INFO | psi.serve:_handle_lookup:232 - lookup\n", "record": {"elapsed": {...}, "extra": {"event": "secret.lookup", ...}, "message": "lookup", "level": {"icon": "ℹ️", "name": "INFO", "no": 20}, "line": 232, ...}}After
{"time": "2026-04-17T05:24:29.387741+00:00", "level": "INFO", "message": "lookup", "module": "serve", "function": "_handle_lookup", "line": 232, "name": "psi.serve", "extra": {"event": "secret.lookup", "secret_id": "...", "provider": "infisical", "outcome": "success", "source": "cache"}}Test plan
pytest tests/test_logging.py tests/test_secret.py— all 22 tests pass;test_logging.pyrewritten to exerciseconfigure_logging(json_output=True)directly and assert on the flat structure.ruff check/ty check— clean.\\nescapes in the body.